package dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("all")
public class BaseDaoImpl<T> implements BaseDao<T>{
	private SessionFactory sessionFactory;
	Class clazz;
	public Session getCurrentSession() {
		if (null==sessionFactory.getCurrentSession()){
			return sessionFactory.openSession();
		}else{
			return sessionFactory.getCurrentSession();
		}		
	}
	public BaseDaoImpl(){
		Class c = getClass();
		Type type = c.getGenericSuperclass();
		if(type instanceof ParameterizedType){
			Type[] types = ((ParameterizedType) type).getActualTypeArguments();
			clazz = (Class) types[0];
		}
	}
	
	@Override
	public boolean save(T t) {
		try
		{
			getCurrentSession().save(t);
			getCurrentSession().clear();
			/*getCurrentSession().close();*/
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean update(T t) {
		try
		{
			Session session = getCurrentSession();
			session.update(t);
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean delete(T t) {
		try
		{
			Session session = getCurrentSession();
			session.delete(t);
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List select(String hql) {
		List list = new ArrayList();
		try
		{
			Session session = getCurrentSession();
			Query query = session.createQuery(hql);
			list = query.list();
			return list;
		}catch(Exception e)
		{
			e.printStackTrace();
			return list;
		}
	}
	
	@Override
	public int excuteBySql(String sql)    
    {    
        int result ;    
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql);    
        result = query.executeUpdate();    
        return result;    
    }
	/* (non-Javadoc)
	 * @see dao.BaseDao#selectBySql(java.lang.String)
	 */
	@Override
	public List<T> selectBySql(String sql) {
		Query q = this.getCurrentSession().createSQLQuery(sql);
		return q.list();
	}

	/* (non-Javadoc)
	 * @see dao.BaseDao#findById(int)
	 */
	@Override
	public T findById(int id) {
		return (T) getCurrentSession().get(getEntity(), id);
	}

	/* (non-Javadoc)
	 * @see dao.BaseDao#findAll()
	 */
	@Override
	public List<T> findAll() { 
		Criteria criteria = getCurrentSession().createCriteria(getEntity());
		return criteria.list();
	}   
	
	protected Class<T> getEntity(){
		Class<T> entityClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		return entityClass;
	}
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
